package com.hsteach.service.visit;

import java.util.List;
import java.util.stream.Collectors;

import com.hsteach.common.core.domain.AjaxResult;
import com.hsteach.common.utils.DateUtils;
import com.hsteach.enums.VisitDataTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hsteach.dao.mapper.visit.VisitFamilyPersonMapper;
import com.hsteach.dao.domain.visit.VisitFamilyPerson;

/**
 * 学生家庭成员信息Service业务层处理
 * 
 * @author fanxquer
 * @date 2023-07-23
 */
@Service
public class VisitFamilyPersonService
{
    @Autowired
    private VisitFamilyPersonMapper visitFamilyPersonMapper;

    /**
     * 查询学生家庭成员信息
     * 
     * @param id 学生家庭成员信息主键
     * @return 学生家庭成员信息
     */
    public VisitFamilyPerson selectVisitFamilyPersonById(Long id)
    {
        return visitFamilyPersonMapper.selectVisitFamilyPersonById(id);
    }

    /**
     * 查询学生家庭成员信息列表
     * 
     * @param visitFamilyPerson 学生家庭成员信息
     * @return 学生家庭成员信息
     */
    public List<VisitFamilyPerson> selectVisitFamilyPersonList(VisitFamilyPerson visitFamilyPerson)
    {
        return visitFamilyPersonMapper.selectVisitFamilyPersonList(visitFamilyPerson);
    }

    public AjaxResult getFamilyPersonInfo(String personId, Long recordId) {
        // 校验是否有基础家庭成员信息
        VisitFamilyPerson query = new VisitFamilyPerson();
        query.setPersonId(personId);
        query.setDataType(VisitDataTypeEnum.STUDENT.getCode());
        List<VisitFamilyPerson> studentPerson = visitFamilyPersonMapper.selectVisitFamilyPersonList(query);

        // 查询家访家庭成员信息，没有则从基础copy一份
        if (recordId != null) {
            query.setDataType(VisitDataTypeEnum.RECORD.getCode());
            query.setRecordId(recordId);
            List<VisitFamilyPerson> recordPerson = visitFamilyPersonMapper.selectVisitFamilyPersonList(query);
            if (recordPerson.size() == 0 && recordPerson.size() != 0) {
                List<VisitFamilyPerson> copyPerson = recordPerson.stream().map(e -> {
                    VisitFamilyPerson person = new VisitFamilyPerson();
                    person.setPersonId(e.getPersonId());
                    person.setPersonType(e.getPersonType());
                    person.setPersonId(e.getPersonId());
                    person.setJob(e.getJob());
                    person.setJobType(e.getJobType());
                    person.setUnitKind(e.getUnitKind());
                    person.setRecordId(recordId);
                    return person;
                }).collect(Collectors.toList());
                visitFamilyPersonMapper.batchInsert(copyPerson);
            } else {
                studentPerson = recordPerson;
            }
        }

        return AjaxResult.success(studentPerson);
    }

    /**
     * 新增学生家庭成员信息
     * 
     * @param visitFamilyPerson 学生家庭成员信息
     * @return 结果
     */
    public int insertVisitFamilyPerson(VisitFamilyPerson visitFamilyPerson)
    {
        visitFamilyPerson.setCreateTime(DateUtils.getNowDate());
        // 先查询是否有基本信息
        VisitFamilyPerson query = new VisitFamilyPerson();
        query.setPersonId(visitFamilyPerson.getPersonId());
        query.setPersonType(visitFamilyPerson.getPersonType());
        query.setDataType(VisitDataTypeEnum.STUDENT.getCode());
        List<VisitFamilyPerson> studentPerson = visitFamilyPersonMapper.selectVisitFamilyPersonList(query);
        // 有则更新，没有就插入
        if (studentPerson.size() > 0) {
            visitFamilyPerson.setId(studentPerson.get(0).getId());
            visitFamilyPersonMapper.updateVisitFamilyPerson(visitFamilyPerson);
        } else {
            visitFamilyPerson.setDataType(VisitDataTypeEnum.STUDENT.getCode());
            visitFamilyPersonMapper.insertVisitFamilyPerson(visitFamilyPerson);
        }

        if (visitFamilyPerson.getRecordId() != null) {
            // 插入记录家庭成员信息
            query.setDataType(VisitDataTypeEnum.RECORD.getCode());
            query.setRecordId(visitFamilyPerson.getRecordId());
            studentPerson = visitFamilyPersonMapper.selectVisitFamilyPersonList(query);
            // 有则更新，没有就插入
            if (studentPerson.size() > 0) {
                visitFamilyPerson.setId(studentPerson.get(0).getId());
                visitFamilyPersonMapper.updateVisitFamilyPerson(visitFamilyPerson);
            } else {
                // 插入记录家庭成员信息
                visitFamilyPerson.setDataType(VisitDataTypeEnum.RECORD.getCode());
                visitFamilyPersonMapper.insertVisitFamilyPerson(visitFamilyPerson);
            }
        }
        return 1;
    }

    /**
     * 修改学生家庭成员信息
     * 
     * @param visitFamilyPerson 学生家庭成员信息
     * @return 结果
     */
    public int updateVisitFamilyPerson(VisitFamilyPerson visitFamilyPerson)
    {
        visitFamilyPerson.setUpdateTime(DateUtils.getNowDate());
        return visitFamilyPersonMapper.updateVisitFamilyPerson(visitFamilyPerson);
    }

    /**
     * 批量删除学生家庭成员信息
     * 
     * @param ids 需要删除的学生家庭成员信息主键
     * @return 结果
     */
    public int deleteVisitFamilyPersonByIds(Long[] ids)
    {
        return visitFamilyPersonMapper.deleteVisitFamilyPersonByIds(ids);
    }

    /**
     * 删除学生家庭成员信息信息
     * 
     * @param id 学生家庭成员信息主键
     * @return 结果
     */
    public int deleteVisitFamilyPersonById(Long id)
    {
        return visitFamilyPersonMapper.deleteVisitFamilyPersonById(id);
    }

    public int deleteVisitFamilyPerson(Long id, Long recordId)
    {
        VisitFamilyPerson person = visitFamilyPersonMapper.selectVisitFamilyPersonById(id);
        // 如果是记录，则id是记录家庭成员信息id,需要删基础信息
        if (recordId != null) {
            visitFamilyPersonMapper.deleteStudentDataByPersonType(person.getPersonId(), person.getPersonType());
        }
        return visitFamilyPersonMapper.deleteVisitFamilyPersonById(id);
    }
}
